package com.xea.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;


public class FileUtil {

	/**
	 * 
	 * @param fromFile
	 * @param toFile
	 * @return
	 */
	public static long copyFile(File fromFile, File toFile) {
		long len = 0;
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new FileInputStream(fromFile);
			out = new FileOutputStream(toFile);
			byte[] t = new byte[1024];
			int ii = 0;
			while ((ii = in.read(t)) > 0) {
				out.write(t, 0, ii);
				len += ii;
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if (out != null) {
				try {
					out.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return len;
	}

	/**
	 * 删除文件,如果是一个文件夹,则删除文件夹下的所有文件
	 * 
	 * @param f
	 * @return
	 */
	public static boolean deleteFile(File f) {
		if (f.exists()) {
			if (f.isFile())
				return f.delete();
			else if (f.isDirectory()) {
				File[] files = f.listFiles();
				for (int i = 0; i < files.length; i++) {
					if (!deleteFile(files[i]))
						return false;
				}
				return f.delete();
			} else
				return false;
		} else
			return false;
	}

	/**
	 * 找出一个文件夹底下的所有文件
	 * 
	 * @param f
	 * @return
	 */
	public static void findAllFile(List<File> files, File f) {
		if (f.exists()) {
			if (f.isFile()) {
				files.add(f);
				return;
			} else if (f.isDirectory()) {
				File[] fs = f.listFiles();
				if (fs == null || fs.length == 0) {
					return;
				}
				for (int i = 0; i < fs.length; i++) {
					findAllFile(files, fs[i]);
				}
			}
		}
	}
	/**
	 * 获取tomcat下的classe路径,如:
	 * 
	 * @return
	 */
	public static String getTomcatClassPath() {
		FileUtil util = new FileUtil();
		String path = util.getClass().getResource("").getPath();
		if (path.indexOf("WEB-INF/classes") > 0) {
			path = path.substring(0, path.indexOf("WEB-INF/classes") + 16);
		}
		return path;
	}
	
	/**
	 * 获取tomcat的路径,如:
	 * 
	 * @return
	 */
	public static String getTomcatPath() {
		FileUtil util = new FileUtil();
		String path = util.getClass().getResource("").getPath();
		System.out.println("path1===:"+path);
		if (path.indexOf("WEB-INF/classes") > 0) {
			//服务器环境需要减7,本地减5
			path = path.substring(0, path.indexOf("WEB-INF/classes"));
		}
		System.out.println("path2===:"+path);
		return path;
	}
	
	public static String getApplicationPath() {
		FileUtil util = new FileUtil();
		String path = util.getClass().getResource("").getPath();
		if (path.indexOf("WEB-INF/classes") > 0) {
			path = path.substring(0, path.indexOf("WEB-INF/classes"));
		}
		return path;
	}
	
	
	public static String validateType(String type){
		String temp="";
		if (type.startsWith("image/gif")) {
			temp = "gif";
		} else if (type.startsWith("image/pjpeg") || type.startsWith("image/jpeg") ) {
			temp = "jpg";
		} else if (type.startsWith("image/bmp")) {
			temp = "bmp";
		} else if (type.startsWith("image/x-png") || type.startsWith("image/png") || type.startsWith("application/x-png")) {
			temp = "png";
		}
		return temp;
	}
	public static String removeSuffix(String fileName) {
		if (fileName.lastIndexOf(".") != -1) {
			fileName = fileName.substring(0, fileName.lastIndexOf("."));
		}
		return fileName;
	}
	public static String getSuffix(String fileName){
		return fileName.substring(fileName.lastIndexOf(".")+1);
	}
	
	public static String getPicUploadPath(){
		String saveBasePath="/pm/";
		String savePath=DateUtil.formatDate(new Date(),"yyyy")+"/"+DateUtil.formatDate(new Date(),"MM");
		return saveBasePath+savePath+"/";
	}
	public static String getTomcatPicUploadPath(){
		String saveBasePath="/pm/";
		String savePath=DateUtil.formatDate(new Date(),"yyyy")+"/"+DateUtil.formatDate(new Date(),"MM");
		return getTomcatPath()+ saveBasePath+savePath+"/";
	}
	public static String getTomcatPicTempUploadPath(){
		return FileUtil.getTomcatPath() + "/upload/";
	}
	public static String getTomcatPicTempZipPath(){
		return FileUtil.getTomcatPath() + "/upload/";
	}
	                                               
	public static String getPicRandomName(){
		String randomFilename="hy"+System.currentTimeMillis();
		return randomFilename;
	}
	
	public static String getBigPicName(String randomName,String ext){
		return randomName+"_b."+ext;
	}
	public static String getMiddlePicName(String randomName,String ext){
		return randomName+"_m."+ext;
	}
	public static String getSmallPicName(String randomName,String ext){
		return randomName+"_s."+ext;
	}
	
}
